package dreadnought;


/*
* Represents a coordinate on the Grid
* Coordinates are represented interally as integers in the
* range 0-9 inclusive
*/
public class GridCoordinate
{
    private int row;
    private int col;

    //this array is used to convert to and from alphanumeric
    //representation of coordinates eg: A4, B6, J1 etc...
    private final char[] chars = {'A','B','C','D','E','F','G','H','I','J'};

    public GridCoordinate()
    {
        this.row = 0;
        this.col = 0;
    }

    public GridCoordinate(int col, int row)
    {
        if (row>9 || row<0 || col>9 || col<0)
            throw new OutOfRangeException("Invalid coordinates.");
        this.row = row;
        this.col = col;
    }

    //this constructor creates a new GridCoordinate from
    //an alphanumeric coordinate string eg: A4, B6, J1
    public GridCoordinate(String alphaNumericCoordinate)
    {
        //slice alphaNumericCoordinate into it's
        //alphabetic and numeric components
        char letter = alphaNumericCoordinate.substring(0,1).charAt(0);
        String number = alphaNumericCoordinate.substring(1);
        
        //find the given letter in the chars list
        //if the letter is not in the chars list col will remain -1
        int col = -1;
        for (int i=0; i<this.chars.length; i++)
        {
            if (this.chars[i] == letter)
                col = i;
        }
        if (col == -1)
            throw new OutOfRangeException("Invalid column letter.");
        this.col = col;

        //parse the numeric component of the alphanumeric coordinate
        int row = Integer.parseInt(number);

        if (row > 9 || row < 0)
            throw new OutOfRangeException("Invalid row.");
        this.col = row;
    }

    public void setRow(int row) 
    {
        if (row > 9 || row < 0)
            throw new OutOfRangeException("Attempted to set invalid row.");
        this.row = row;
    }

    public int getRow()
    {
        return this.row;
    }

    public void setCol(int col) 
    {
        if (col > 9 || col < 0)
            throw new OutOfRangeException("Attempted to set invalid column.");
        this.col = col;
    }

    public int getCol()
    {
        return this.col;
    }

    //returns a string representation of this coordinate
    //eg: if col=0 and row=0 the string "A1" is returned
    //eg: if col=2 and row=3 the string "C4" is returned
    //eg: if col=9 and row=9 the string "J10" is returned
    @Override
    public String toString()
    {
        return Character.toString(this.chars[this.col]) + (this.row+1);
    }

    //returns a hashCode for this object which will be the same
    //for two different instances only if they have equivelant row and col
    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + this.row;
        hash = 97 * hash + this.col;
        return hash;
    }

    //returns true iff this GridCoordinate is equal two another
    //meaning that they hold the same row and column values
    @Override
    public boolean equals(Object other)
    {
        boolean result = false;
        if (other instanceof GridCoordinate)
        {
            GridCoordinate that = (GridCoordinate) other;
            result = (this.getRow() == that.getRow()
                    && this.getCol() == that.getCol());
        }
        return result;
    }
}